package com.sk.openapicallexample_android;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sk.openapicallexample_android.http.RetrofitFactory;
import com.sk.openapicallexample_android.http.bean.normalbean.BaseEntity;
import com.sk.openapicallexample_android.http.config.HttpConfig;



import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by yemao on 2017/6/12.
 */
public class MainActivity extends AppCompatActivity {
    private TextView mTvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle(R.string.main_activity_title);
        mTvResult = (TextView) findViewById(R.id.tv_result);
    }

    public void btnEquipmentList(View v) {

        final ProgressDialog mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage(getResources().getString(R.string.progress_load_msg));

        //模拟获取设备列表
        RetrofitFactory.getInstence().API()
                .getWipmList(HttpConfig.UUID)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mProgressDialog.show();
                    }

                    @Override
                    public void onNext(BaseEntity<Object> objectBaseEntity) {
                        mProgressDialog.dismiss();
                        Log.e("", "成功");

                        Gson mGson = new Gson();
                        String result = mGson.toJson(objectBaseEntity);

                        mTvResult.setText(result);
//                      mTvResult.setText(objectBaseEntity.getData().toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("", "失败" + e);
                        mTvResult.setText(e + "");
                        mProgressDialog.dismiss();
                    }

                    @Override
                    public void onComplete() {
//                        mProgressDialog.dismiss();
                    }
                });

    }
}
